﻿using Function;
using System;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UI.Permission;
using UI.UserControl.Base;

namespace UI.UserControl.UI
{
    public partial class DetailPage : DetailPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var mid = Request.QueryString["mid"] ?? "";
                if (!RightCheck.HavePermission(RightCheck.permissionType.View))
                {
                    MessageBox box = new MessageBox();
                    box.ErrorBox(Page, "没有权限", Method.GetHomeBaseUrl("list.aspx?mid=" + mid));
                    return;
                }
                if (Jumpurl.IsEmpty())
                    Jumpurl = Method.ParaListUrl;
            }
        }

        /// <summary>
        /// Gets a collection of System.Web.UI.WebControls.DataControlField objects that
        ///     represent the explicitly declared row fields in a System.Web.UI.WebControls.DetailsView
        ///     control.
        /// </summary>
        public DataControlFieldCollection Fields = new DataControlFieldCollection();

        /// <summary>
        /// 数据源
        /// </summary>
        public object dataSource { set { DetailsView1.DataSource = value; } private get { return DetailsView1.DataSource; } }

        /// <summary>
        /// 绑定事件
        /// </summary>
        public event EventHandler dataBind;

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void Bind()
        {
            DetailsView1.DataBind();
            //绑定后自定义html
            foreach (DetailsViewRow item in DetailsView1.Rows)
            {
                if (item.Cells[0] != null && item.Cells[1] != null)
                {
                    if (item.Cells[0].Text.IsNotEmpty() && !item.Cells[0].Text.Contains("&nbsp;"))
                    {
                        var value = item.Cells[1].Text.Trim().Replace("&nbsp;", "");
                        if (IgnoreEmptyValue && value.IsEmpty())
                            continue;
                        Html += string.Format(divTemplate, item.Cells[0].Text, item.Cells[1].Text);
                    }
                }
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="t"></param>
        /// <param name="source"></param>
        public void InitData(Type t, object source)
        {
            Type type = t;
            PropertyInfo pi = type.GetProperties()[0];
            foreach (var p in pi.PropertyType.GetProperties())
            {
                if (p == null)
                    continue;
                this.Fields.Add(new BoundField { DataField = p.Name, HeaderText = p.Name });
            }
            this.dataSource = source;
            Bind();
        }

        /// <summary>
        /// 返回页面
        /// </summary>
        public string Jumpurl;

        protected void Event_dataBind(object sender, EventArgs e)
        {
            if (dataBind != null)
            {
                dataBind(this, e);
            }
        }
    }
}